package com.magic.smartcity.view.more.bus

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.Navigation
import com.magic.smartcity.R
import com.magic.smartcity.beans.BusOrderBean
import com.magic.smartcity.databinding.FragmentBusRidingTimeBinding
import java.util.*

class BusRidingTimeFragment : Fragment() {

    private lateinit var binding: FragmentBusRidingTimeBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentBusRidingTimeBinding.inflate(layoutInflater)
        initView()
        return binding.root


    }

    //选择日期后进行执行的操作
    private val dateSetListener: DatePickerDialog.OnDateSetListener =
        DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
            val date = "${year}-${month + 1}-${dayOfMonth}"
            binding.ridingDate.setText(date)
        }

    //选择时间后进行执行的操作
    private val timeSetListener: TimePickerDialog.OnTimeSetListener =
        TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
            val time = "${hourOfDay}:${minute}"
            binding.ridingTime.setText(time)
        }

    //显示日期和时间的组件，默认选中现在的日期|时间
    val calendar: Calendar = Calendar.getInstance()
    val selectDateLambda = {
        val datePickerDialog = DatePickerDialog(
            requireContext(),
            android.R.style.Theme_Material_Dialog_MinWidth,
            dateSetListener,
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)
        )
        datePickerDialog.show()
    }
    val selectTimeLambda = {
        val timePickerDialog = TimePickerDialog(
            requireContext(),
            android.R.style.Theme_Material_Dialog_MinWidth,
            timeSetListener,
            calendar.get(Calendar.HOUR),
            calendar.get(Calendar.MINUTE),
            true
        )
        timePickerDialog.show()
    }

    private fun initView() {
        //初始化日期选择
        //选择日期时，初始化默认选中的是系统当前日期

        binding.ridingDate.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) selectDateLambda()
        }
        binding.ridingTime.setOnClickListener { selectDateLambda() }
        //初始化时间选择
        //选择时间时，初始化默认选中当前系统时间
        binding.ridingTime.setOnFocusChangeListener { _, hasFocus ->
            if (hasFocus) selectTimeLambda()
        }
        binding.ridingTime.setOnClickListener { selectTimeLambda() }

        //下一步
        binding.goToSelectRidingPassenger.setOnClickListener{
            when{
                binding.ridingDate.text!=null&&binding.ridingDate.text.isNullOrEmpty()->{
                    binding.ridingDate.error = "乘车日期不能为空"
                }
                binding.ridingTime.text!=null&&binding.ridingTime.text.isNullOrEmpty()->{
                    binding.ridingTime.error = "乘车时间不能为空"
                }
                else->{
                    val str = binding.ridingDate.text.toString()+" "+binding.ridingTime.text.toString()
                    val bundle = Bundle()
                    val busOrderBean = BusOrderBean()
                    busOrderBean.ridingTime = str
                    bundle.putSerializable("busOrderBean",busOrderBean)
                    bundle.putInt("lineId",arguments?.getInt("lineId")!!)
                    Navigation.findNavController(it).navigate(R.id.action_busRidingTimeFragment_to_busRidingPassengerFragment,bundle)
                }
            }
        }
    }

}